package com.patchmanage.back.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.patchmanage.back.entity.OverviewCharts;
import com.patchmanage.back.entity.Patch;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IInventoryService;
import com.patchmanage.back.service.IOverviewChartsService;
import com.patchmanage.back.service.IPatchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  概览统计
 * </p>
 *
 * @author huizhao
 * @since 2021-05-08
 */
@Api(value = " 数据统计 controller", tags = {"数据统计 Api"})
@RestController
@RequestMapping("/back/overviewcharts")
public class OverviewChartsController {

    @Autowired
    private IInventoryService inventoryService;

    @Autowired
    private IPatchService patchService;

    @Autowired
    private IOverviewChartsService iOverviewChartsService;


    /**
     * 获取统计表列表
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取统计表列表接口", notes = "获取统计表列表接口")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<List<OverviewCharts>> list(@RequestBody Dict dict) {
        QueryWrapper<OverviewCharts> queryWrapper = new QueryWrapper<OverviewCharts>();
        List<OverviewCharts> groupList = iOverviewChartsService.list(queryWrapper);
        return Result.success(groupList);
    }



    /**
     * 数据统计接口
     * @return
     */
    @RequestMapping(value = "dataCount", method = RequestMethod.POST)
    @ApiOperation(value = "数据统计接口", notes = "数据统计接口")
    public Result<Dict> dataCount() {

        /**漏洞资产总量**/
       Integer inventoryCount =  inventoryService.count();
        /**补丁库总量**/
       Integer patchCount =  patchService.count();


       QueryWrapper<Patch> queryWrapper = new QueryWrapper();
       Date now = new Date();
       Date startDate =  DateUtil.beginOfMonth(now);
       Date endDate =  DateUtil.endOfMonth(now);
       DateUtil.beginOfMonth(now);
       queryWrapper.between("publishing_time",startDate,endDate);

       /**本月新增补丁**/
       Integer incrementPatchCountMonth =  patchService.count(queryWrapper);


       QueryWrapper<Patch> queryFormalWrapper = new QueryWrapper();
       queryFormalWrapper.eq("is_official_mark",1);
       queryFormalWrapper.between("publishing_time",startDate,endDate);
       /**本月轉正补丁**/
       Integer formalPatchCountMonth =  patchService.count(queryFormalWrapper);

       Dict returnDict = new Dict();
       returnDict.put("inventoryCount",inventoryCount);
       returnDict.put("patchCount",patchCount);
       returnDict.put("incrementPatchCountMonth",incrementPatchCountMonth);
       returnDict.put("formalPatchCountMonth",formalPatchCountMonth);

       return Result.success(returnDict);
    }
}
